using System;
using System.Collections.Generic;
using System.Text;

namespace CasinoServer.BusinessEntities
{
    public class tmpCardDeck
    {
        private Dictionary<int, tmpCard> cards;

        public tmpCardDeck()
        {
            NewGame();
        }

        private tmpCard TranslateIntIntTotmpCard(int face, int color)
        {
            tmpCard returnValue = null;

            if (face > 12 || face < 0 || color > 3 || color < 0)
                throw new ArgumentException();

            tmpCardColor cc = new tmpCardColor();
            tmpCardFace cf = new tmpCardFace();

            switch (color)
            {
                case 0: cc = tmpCardColor.Clubs; break;
                case 1: cc = tmpCardColor.Spades; break;
                case 2: cc = tmpCardColor.Diamonds; break;
                case 3: cc = tmpCardColor.Hearts; break;
            }

            switch (face)
            {
                case 0: cf = tmpCardFace.two; break;
                case 1: cf = tmpCardFace.three; break;
                case 2: cf = tmpCardFace.four; break;
                case 3: cf = tmpCardFace.five; break;
                case 4: cf = tmpCardFace.six; break;
                case 5: cf = tmpCardFace.seven; break;
                case 6: cf = tmpCardFace.eight; break;
                case 7: cf = tmpCardFace.nine; break;
                case 8: cf = tmpCardFace.ten; break;
                case 9: cf = tmpCardFace.Jack; break;
                case 10: cf = tmpCardFace.Queen; break;
                case 11: cf = tmpCardFace.King; break;
                case 12: cf = tmpCardFace.Ace; break;
            }
            returnValue = new tmpCard(cf, cc);
            return returnValue;
        }

        public void SortDeck(int seed)
        {
            List<int> keys = new List<int>();
            List<int> positions = new List<int>();
            Random rand = new Random(seed);
            Dictionary<int, tmpCard> newDeck = new Dictionary<int, tmpCard>();

            for (int i = 0; i < 52; i++)
            {
                keys.Add(i);
                positions.Add(i);
            }

            while (keys.Count != 0)
            {
                DateTime x = DateTime.Now;
                rand = new Random(x.Millisecond * x.Second * x.Minute * x.Day);

                int key = rand.Next(x.Hour * x.Millisecond) % keys.Count;
                System.Threading.Thread.Sleep(1);
                int pos = rand.Next(x.Day * x.Second) % positions.Count;
                key = keys[key];
                pos = positions[pos];

                if (keys.Contains(key) && positions.Contains(pos))
                {
                    tmpCard tmp = new tmpCard();
                    if (!cards.TryGetValue(key, out tmp))
                        throw new Exception("shuffle eror");
                    cards.Remove(key);
                    keys.Remove(key);
                    positions.Remove(pos);
                    newDeck.Add(pos, tmp);
                }
            }
            cards.Clear();
            cards = newDeck;
        }

        public tmpCard GetFirst()
        {
            tmpCard tmp = null;
            for (int i = 0; i < 52; i++)
            {
                if (cards.TryGetValue(i, out tmp))
                {
                    cards.Remove(i);
                    break;
                }
            }
            return tmp;
        }

        public void NewGame()
        {
            cards = new Dictionary<int, tmpCard>();
            int key = 0;
            for (int i = 0; i < 4; i++)
                for (int j = 0; j < 13; j++)
                {
                    cards.Add(key, TranslateIntIntTotmpCard(j, i));
                    key++;
                }
        }

        private int GetListMaxValue(List<int> list)
        {
            for (int i = 51; i >= 0; i--)
            {
                if (list.Contains(i))
                {
                    return i;
                }
            }
            throw new Exception("couldnt find max value in list<int>");
        }
    }
}
